package com.bsoft.gol.hbs.service.rpc.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsoft.gol.hbs.bean.qo.DictionaryQo;
import com.bsoft.gol.hbs.service.rpc.DictionaryRpcServiceIntf;
import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.controller.exception.ControllerException;
import ctd.dictionary.DictionaryItem;
import ctd.dictionary.controller.DictionaryController;
import ctd.spring.boot.annotation.RpcAccess;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.dictionary.qo.QueryDictionaryListQo;
import hcn.base.dictionary.vo.QueryDictionaryListVo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ChenJij on 2022/1/13.
 */
@SsdevService("dictionaryRpcService")
public class DictionaryRpcServiceImpl implements DictionaryRpcServiceIntf {

    @Override
    @RpcService
    @RpcAccess(auth = false)
    public List<QueryDictionaryListVo> queryDictionaryList(QueryDictionaryListQo qo) {
        List<QueryDictionaryListVo> queryDictionaryListVos = new ArrayList<>();
        if (CommonUtils.isNotEmpty(qo.getCodeList())) {
            //检索关键字
            String keywords = qo.getKeywords();
            for (String code : qo.getCodeList()) {
                QueryDictionaryListVo queryDictionaryListVo = new QueryDictionaryListVo();
                queryDictionaryListVo.setCode(code);

                ctd.dictionary.Dictionary sourceDic = null;
                try {
                    sourceDic = DictionaryController.instance().get(code);
                } catch (ControllerException e) {
                    e.printStackTrace();
                }

                if (null != sourceDic) {
                    queryDictionaryListVo.setText(sourceDic.getName());
                    if (null != sourceDic) {
                        List<DictionaryItem> slice = sourceDic.getSlice(null, 0, null);
                        if (CommonUtils.isEmpty(keywords)) {
                            queryDictionaryListVo.setItems(slice);
                        } else if (CommonUtils.isNotEmpty(slice)) {
                            List<DictionaryItem> items = new ArrayList<>();
                            for (DictionaryItem item :
                                    slice) {
                                JSONObject itemJsonObject = JSON.parseObject(JSON.toJSONString(item));
                                //instr 包含编码、名称、拼音、五笔。都可以进行检索
                                if (itemJsonObject.getString("instr") != null && itemJsonObject.getString("instr").contains(keywords)) {
                                    items.add(item);
                                }
                            }
                            queryDictionaryListVo.setItems(items);
                        }
                    }
                }

                queryDictionaryListVos.add(queryDictionaryListVo);
            }
        }
        return queryDictionaryListVos;
    }

    /**
     * 获取指定节点下字典项
     * @param dictionaryQo
     * @return
     */
    @Override
    @RpcService
    public List<DictionaryItem> getDictionarySlice(DictionaryQo dictionaryQo) {
        List<DictionaryItem> items = null;
        try {
            items = DictionaryController.instance().get(dictionaryQo.getDictionaryName()).getSlice(dictionaryQo.getParentKey(),
                    dictionaryQo.getSliceTypes(), dictionaryQo.getItemName());
        } catch (ControllerException e) {
            e.printStackTrace();
        }

        return items;
    }


    /**
     * @description: 无需token获取字典列表
     * @author: chenJij@bsoft.com.cn
     * @date: 2023/7/12 14:47
     * @param: qo
     * @return: java.util.List<hcn.base.dictionary.vo.QueryDictionaryListVo>
     */
    @Override
    @RpcService
    @RpcAccess(auth = false)
    public List<QueryDictionaryListVo> queryDictionaryListWithoutToken(QueryDictionaryListQo qo) {
        return queryDictionaryList(qo);
    }

    /**
     * @description: 无需token获取字典子项列表
     * @author: chenJij@bsoft.com.cn
     * @date: 2023/7/12 14:48
     * @param: dictionaryQo
     * @return: java.util.List<ctd.dictionary.DictionaryItem>
     */
    @Override
    @RpcService
    @RpcAccess(auth = false)
    public List<DictionaryItem> getDictionarySliceWithoutToken(DictionaryQo dictionaryQo) {
        return getDictionarySlice(dictionaryQo);
    }

}